package com.haha.community.controller;


import com.haha.community.annotation.LoginRequire;
import com.haha.community.entity.Comment;
import com.haha.community.entity.DiscussPost;
import com.haha.community.entity.Page;
import com.haha.community.entity.User;
import com.haha.community.service.CommentService;
import com.haha.community.service.DiscussPostService;
import com.haha.community.service.UserService;
import com.haha.community.util.CommunityConstant;
import com.haha.community.util.CommunityUtil;
import com.haha.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Yang
 * @since 2020-10-30
 */
@Controller
public class DiscussPostController implements CommunityConstant {
    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private UserService userService;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private CommentService commentService;

    /**
     * page中只需要传入当前页码和每页条数，其他的后端计算
     * @param model
     * @param page
     * @return
     */
    @GetMapping("/index")
    public String getIndexPage(Model model, Page page) {
        // 方法调用钱,SpringMVC会自动实例化Model和Page,并将Page注入Model,不需要再次添加
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        int count = discussPostService.findDiscussPostRow(0);
        page.setRows(count);
        //在path中会拼接参数复用
        page.setPath("/index");
        List<DiscussPost> Posts = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit());
        for (DiscussPost post : Posts) {
            Map<String, Object> map = new HashMap<>();
            //后续考虑多表查询优化，前端页面也需要优化
            map.put("post", post);
            User user = userService.getById(post.getUserId());
            map.put("user", user);
            discussPosts.add(map);
        }
        model.addAttribute("discussPosts", discussPosts);
        return "/index";
    }

    @PostMapping("/discuss/add")
    @ResponseBody
    public String addDiscussPost(String title, String content) {
        User user = hostHolder.getUser();
        if (user == null) {
            return CommunityUtil.getJson("403","请登陆后在进行该操作");
        }

        DiscussPost post = new DiscussPost();
        post.setUserId(String.valueOf(user.getId()));
        post.setTitle(title);
        post.setContent(content);
        discussPostService.addDiscussPost(post);

        // 报错的情况,将来统一处理.
        return CommunityUtil.getJson("0","发布成功");
    }


    @RequestMapping(path = "/discuss/detail/{discussPostId}", method = RequestMethod.GET)
    public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page) {
        // 帖子
        DiscussPost post = discussPostService.getById(discussPostId);
        model.addAttribute("post", post);
        // 作者
        User user = userService.getById(post.getUserId());
        model.addAttribute("user", user);
        // 评论分页信息
        page.setLimit(5);
        page.setPath("/discuss/detail/" + discussPostId);
        page.setRows(post.getCommentCount());
        //帖子评论
        List<Comment> commentList = commentService.findCommentsByEntity(ENTITY_TYPE_POST, post.getId(), page.getOffset(), page.getLimit());
        List<Map<String,Object>> commentVoList = new ArrayList<>();
        if (commentList!=null) {
            for (Comment comment : commentList) {
                Map<String, Object> map = new HashMap<>();
                map.put("comment", comment);
                User user1 = userService.getById(comment.getUserId());
                map.put("user",user1);
                // 回复VO列表
                List<Map<String, Object>> replyVoList = new ArrayList<>();
                //评论的回复列表
                List<Comment> replys = commentService.findCommentsByEntity(ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE);
                if (replys!=null) {
                    for (Comment reply : replys) {
                        Map<String, Object> replyVo = new HashMap<>();
                        // 回复
                        replyVo.put("reply", reply);
                        // 作者
                        replyVo.put("user", userService.getById(reply.getUserId()));
                        // 回复目标
                        User target = reply.getTargetId() == 0 ? null : userService.getById(reply.getTargetId());
                        replyVo.put("target", target);
                        replyVoList.add(replyVo);
                    }
                }
                map.put("replys",replyVoList);
                // 回复数量
                int replyCount = commentService.findCommentCount(ENTITY_TYPE_COMMENT, comment.getId());
                map.put("replyCount", replyCount);
                commentVoList.add(map);
            }
        }
        model.addAttribute("comments", commentVoList);

        return "/site/discuss-detail";
    }

}

